﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace Dragon.Prototype1.Infrastructure.Controls
{
    public abstract class RepeatControl :jQueryControl
    {
        private List<string> repeatItemsValues = new List<string>();
        public List<string> RepeatItemsValues
        {
            get { return repeatItemsValues; }
        }
        private List<string> repeatItemsLabels = new List<string>();
        public List<string> RepeatItemsLabels
        {
            get { return repeatItemsLabels;}
        }
        protected List<string> repeatItemsIds = new List<string>();

        public string GroupName
        {
            get;
            set;
        }

        protected List<WebControl> controlItems = new List<WebControl>();

        public string RepeatItemCssClass
        {
            get;
            set;
        }

        public override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            this.OnPreRender(writer);
            this.RenderRepeatContents(writer);
        }

        public string ValidationMessage
        {
            get;
            set;
        }

        protected abstract void RenderRepeatContents(HtmlTextWriter writer);

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Unknown;
            }
        }
    }

    public class CheckBoxList : RepeatControl
    {
        private List<bool> isChecked = new List<bool>();
        public List<bool> IsChecked
        {
            get { return isChecked; }
        }

        public CheckBoxList(string groupName,string repeatItemCssClass)
        {
            this.GroupName = groupName;
            this.RepeatItemCssClass = repeatItemCssClass;
            this.ValidationMessage = "This field is required";
        }

        public void DataBind(List<string> labels, List<string> values,List<bool> ischecked)
        {
            if (labels.Count != values.Count)
            {
                throw new ArgumentException("the number of labels doesn't match with the number of values");
            }
            RepeatItemsValues.Clear();
            RepeatItemsLabels.Clear();
            IsChecked.Clear();
            repeatItemsIds.Clear();

            for (int i = 0; i < labels.Count; i++)
            {
                RepeatItemsLabels.Add(labels[i]);
                RepeatItemsValues.Add(values[i]);
                if (ischecked != null)
                    IsChecked.Add(ischecked[i]);
                repeatItemsIds.Add(string.Format("{0}_{1}", this.GroupName, i));
            }
        }

        protected override void RenderRepeatContents(HtmlTextWriter writer)
        {
            foreach (WebControl item in controlItems)
            {
                item.Render(writer);
            }
            writer.AddAttribute(HtmlTextWriterAttribute.For, this.GroupName);
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "error");
            writer.RenderBeginTag(HtmlTextWriterTag.Label);
            writer.Write(this.ValidationMessage);
            writer.RenderEndTag();
        }

        public CheckBoxList(string groupName):this(groupName,string.Empty)
        {
        
        }

        protected override void OnPreRender(HtmlTextWriter writer)
        {

            for (int i = 0; i < this.RepeatItemsLabels.Count;i++ )
            {
                CheckBox newCheckBox;
                if(IsChecked.Count>0)
                    newCheckBox = new CheckBox(this.repeatItemsIds[i],this.GroupName,RepeatItemsLabels[i],RepeatItemsValues[i],IsChecked[i]);
                else
                    newCheckBox = new CheckBox(this.repeatItemsIds[i], this.GroupName, RepeatItemsLabels[i], RepeatItemsValues[i], false);

                if (!string.IsNullOrEmpty(this.RepeatItemCssClass))
                {
                    newCheckBox.CssClass += " " + RepeatItemCssClass;
                }
                controlItems.Add(newCheckBox);
            }
        }
    }

    //public class RadioBoxList : RepeatControl
    //{
    //    public RadioBoxList(string groupName,string repeatItemCssClass)
    //    {
    //        this.GroupName = groupName;
    //        this.RepeatItemCssClass = repeatItemCssClass;
    //    }

    //    public RadioBoxList(string groupName)
    //        : this(groupName, string.Empty)
    //    {
        
    //    }


    //    protected override void OnPreRender(HtmlTextWriter writer)
    //    {
    //        foreach (object itemValue in this.RepeatItemsValues)
    //        {
    //            RadioBox newCheckBox = new RadioBox(this.GroupName, (bool)itemValue);
    //            if (string.IsNullOrEmpty(this.RepeatItemCssClass))
    //            {
    //                newCheckBox.CssClass += " " + RepeatItemCssClass;
    //            }
    //            controlItems.Add(newCheckBox);
    //        }
    //    }
    //}
}
